package br.com.utilidades;
 
import java.util.Map;

import javax.faces.context.FacesContext;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

/**
 * @version 1.0.0
 * @since   December,12 2011
 * @author  Eduardo Francisco Barbosa 
 */
public class ViewScope implements Scope {
	
	/**
     * StartService
     * 
     * @param String name
     * @param ObjectFactory<?> objectFactory
     * 
     * @return Object
     */
	public Object get(String name, ObjectFactory<?> objectFactory) {
		Map<String,Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
		if(viewMap.containsKey(name)) {
			return viewMap.get(name);
		 } else {
		    Object object = objectFactory.getObject();
		    viewMap.put(name, object);
		    return object;
        }
	}

    /**
     * remove
     * 
     * @param String name
     * 
     * @return Object
     */
    public Object remove(String name) {
        return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
    }

    /**
     * getConversationId
     * 
     * @return String
     */
    public String getConversationId() {
        return null;
    }

    /**
     * registerDestructionCallback
     * 
     * @param String name
     * @param Runnable callback
     */
    public void registerDestructionCallback(String name, Runnable callback) {
    }

    /**
     * resolveContextualObject
     * 
     * @param String key
     * 
     * @return Object
     */
    public Object resolveContextualObject(String key) {
        return null;
    }

}
